﻿$(document).ready(function() {
    var today = new Date();

    $(".from-date.datepicker").datepicker({
        //        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        showOn: "button",
        maxDate: today,
        onClose: function(selectedDate) {
            $(".to-date.datepicker").datepicker("option", "minDate", selectedDate);
        }
    });
    $(".to-date.datepicker").datepicker({
        //        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        showOn: "button",
        maxDate: today,
        onClose: function(selectedDate) {
            $(".from-date.datepicker").datepicker("option", "maxDate", selectedDate);
        }
    });

    $('#centeredmenu li a').click(function () {
        if ($(this).closest('li').hasClass('current')) return;
        status = $(this).attr('class');
        loadData(1);

        $('#centeredmenu li.current').removeClass('current');
        $(this).closest('li').addClass('current');
    });

    $('#button-find').click(function() {
        loadData(1);
    });
    loadData(1);
});

var status = 'pending';
var currentPage, pageNum;

function loadData(pageNum, isPaged) {
    var result = requestFormImplement.getApprovalList(status, pageNum,
        $('#keyword').val());

    if (result != null) {
        $('#list').setTemplateURL("/templates/request-list.htm");
        $('#list').processTemplate(result);

        if (!isPaged) {
            paginator(result.RowCount, result.PageSize);
        }

        if (result.CountPending > 0) {
            $("#countPending").text(' (' + result.CountPending + ')');
        } else {
            $("#countPending").text('');
        }
        if (result.CountApproval > 0) {
            $("#countApproval").text(' (' + result.CountApproval + ')');
        } else {
            $("#countApproval").text('');
        }
        if (result.CountDeleted > 0) {
            $("#countDeleted").text(' (' + result.CountDeleted + ')');
        } else {
            $("#countDeleted").text('');
        }
    }

    currentPage = pageNum;
}

function paginator(rowCount, pageSize) {
    $('#pager').smartpaginator({
        totalrecords: rowCount,
        recordsperpage: pageSize,
        length: 5,
        next: 'Next',
        prev: 'Prev',
        first: 'First',
        last: 'Last',
        theme: 'blue',
        controlsalways: true,
        onchange: function(newPage) {
            loadData(newPage, true);
            currentPage = newPage;
            $('#r').html('Page # ' + newPage);
        }
    });
}

var requestFormImplement = new Object(
    {
        getItem: function(id) {
            var item;
            $.ajax({
                type: "POST",
                url: "/Pages/Default.aspx/GetItem",
                data: JSON.stringify({ id: id }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function(data) {
                    item = data.d;
                },
                error: function() {
                    item = null;
                }
            });

            return item;
        },
        updateItem: function(item) {
            var result;
            $.ajax({
                type: "POST",
                url: "/Pages/Default.aspx/UpdateItem",
                data: JSON.stringify({ item: item }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function(data) {
                    result = data.d;
                },
                error: function() {
                    result = false;
                }
            });

            return result;
        },
        getList: function(status, pageNumber, keyword) {
            var list;
            $.ajax({
                type: "POST",
                url: "/Pages/Default.aspx/GetList",
                data: JSON.stringify({ status: status, pageNumber: pageNumber, keyword: keyword }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function(data) {
                    list = data.d;
                },
                error: function() {
                    list = null;
                }
            });

            return list;
        },
        getApprovalList: function(status, pageNum, keyword) {
            var list;
            $.ajax({
                type: "POST",
                url: "/Pages/Default.aspx/GetApprovalList",
                data: JSON.stringify({ status: status, pageNum: pageNum, keyword: keyword }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function(data) {
                    list = data.d;
                },
                error: function() {
                    list = null;
                }
            });

            return list;
        },
        deleteItems: function(ids, status) {
            var result;
            $.ajax({
                type: "POST",
                url: (status === 'deleted' ? "/Pages/Default.aspx/DestroyItems" :
                    "/Pages/Default.aspx/DeleteItems"),
                data: JSON.stringify({ ids: ids }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function(data) {
                    result = data.d;
                },
                error: function() {
                    result = false;
                }
            });

            return result;
        },
        undo: function(ids) {
            var result;
            $.ajax({
                type: "POST",
                url: "/Pages/Default.aspx/UndoItems",
                data: JSON.stringify({ ids: ids }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function(data) {
                    result = data.d;
                },
                error: function() {
                    result = false;
                }
            });

            return result;
        }
    });